#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace hc
{
	template <class T>
	struct list_node
	{
		list_node<T>* prev;
		list_node<T>* next;
		T data;
		list_node(const T&x=T())
			:prev(nullptr),next(nullptr),data(x)
		{}

	};

	template <class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> node;
		typedef list_iterator<T, Ref, Ptr> self;
		node* _node;

		list_iterator(node* n)
		{
			_node = n;
		}
		Ref operator*()
		{
			return _node->data;
		}
		Ptr operator->()
		{
			return &_node->data;
		}
		self& operator++()
		{
			_node = _node->next;
			return *this;
		}
		self& operator++(int)
		{
			self tmp(*this);
			_node = _node->next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->prev;
			return *this;
		}
		self& operator--(int)
		{
			self tmp(*this);
			_node = _node->prev;
			return tmp;
		}
		bool operator!=(const self &s)
		{
			return _node != s._node;
		}
		bool operator==(const self& s)
		{
			return _node == s._node;
		}
	};

	template <class T>
	class list
	{
		
	public:
		typedef list_node<T> node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return iterator(head->next);
		}
		const_iterator begin()const
		{
			return iterator(head->next);
		}
		iterator end()
		{
			return iterator(head);
		}
		const_iterator end()const
		{
			return iterator(head);
		}
		void empty_init()
		{
			head = new node;
			head->next = head;
			head->prev = head;
		}
		list()
		{
			empty_init();
		}

		template <class Iterator>
		list(Iterator first, Iterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		void swap(list<T>& tmp)
		{
			std::swap(head, tmp.head);
		}
		list(const list<T>& lt)
		{
			empty_init();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		~list()
		{
			clear();
			delete head;
			head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(end());
		}
		void pop_front()
		{
			erase(begin());
		}
		void insert(iterator pos, const T& x)
		{
			node* cur = pos._node;
			node* prev = cur->prev;
			node* newnode = new node(x);

			prev->next = newnode;
			newnode->prev = prev;
			newnode->next = cur;
			cur->prev = newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._node->prev;
			node* next = pos._node->next;
			prev->next = next;
			next->prev = prev;
			delete pos._node;
			return iterator(next);
		}


	private:
		node *  head;
	};
	
}
